/*
 * MainMenu.java
 *
 * Created on May 15, 2008, 2:06 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package morseme;

/**
 *
 * @author tt
 */
import javax.microedition.lcdui.*;

public class MainMenu extends List implements CommandListener {

// private static final Command selCmd = new Command("Select", Command.ITEM, 1);
    private static final Command exitCmd = new Command("Exit", Command.EXIT, 1);
    private Display display;
    private Option option;
    private Inbox inbox;
    private SMSClient client;
    private MorseTest test;
    private PhoneBook pBook;
    private UserTypeMenu usrTypeMenu;
    private MorseME midlet;

    public MainMenu() {
        
        super("MorseME Main Menu", List.IMPLICIT);
        this.display = Display.getDisplay(MorseME.getMIDlet());
        this.midlet = MorseME.getMIDlet();

        this.append("Send Message", null);
        this.append("Recieve Message", null);
        this.append("Setting", null);
        this.append("Test", null);
//this.append("Phone Book", null);

        this.addCommand(exitCmd);
        this.setCommandListener(this);

        inbox = new Inbox(this,this.midlet);
    }

    public void commandAction(Command c, Displayable d) {

        if (c == List.SELECT_COMMAND) {

            if (this.getSelectedIndex() == 0) {
//New Message
                if (client == null) {

                    if (pBook == null) {
                        pBook = new PhoneBook(this);
                    }

                    client = new SMSClient(this, pBook);

                } else {

                    client.refresh();
                }

                this.display.setCurrent(client);

            } else if (this.getSelectedIndex() == 1) {
//Inbox
                if (inbox == null) {

                    inbox = new Inbox(this,this.midlet);

                } else {

                    inbox.updateInbox();
                }
                
                if(this.usrTypeMenu == null){
                    this.usrTypeMenu = new UserTypeMenu(this, this.inbox, this.midlet);
                }

                this.display.setCurrent(this.usrTypeMenu);

            } else if (this.getSelectedIndex() == 2) {

//Setting
                if (option == null) {
                    option = new Option(this);
                } else {
                    option.refresh();
                }
                
                this.display.setCurrent(this.option);

            } else if (this.getSelectedIndex() == 3) {
//Test
                if (test == null) {

                    test = new MorseTest(this,this.midlet);

                } else {

                    test.refresh();
                }

                this.display.setCurrent(test);

            } else if (this.getSelectedIndex() == 4) {
//Phone Book
                if (pBook == null) {

                    pBook = new PhoneBook(this);

                }

                this.display.setCurrent(pBook);

            }
        } else if (c == exitCmd) {

            MorseME.getMIDlet().quitApp();
        }
    }
}

